package com.kpiJavaTraining.bogdan.module2;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;


public class AddQuestion {

	
	private static OutputStream out = null;
	private static ObjectOutputStream objectOut = null;
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
	
		out = new FileOutputStream("question.quest");
		objectOut = new ObjectOutputStream(out);
		Reader reader = new Reader();
	
		loop:while(true){
			System.out.print("one - o  many - m  string - s  exit - e :");
			switch(reader.readLn()){
			case "o" : objectOut.writeObject(createQuestion("one"));break;
			case "m" : objectOut.writeObject(createQuestion("many"));break;
			case "s" : objectOut.writeObject(createQuestion("string"));break;
			case "e" : break loop;
			}
		}
		
	objectOut.flush();
	out.flush();
	objectOut.close();
	out.close();
	}
		
	private static Question createQuestion(String type){
		switch(type){
		case "one"   : return createOneAnswer();
		case "many"  : return createManyAnswers();
		case "string": return createString();
		}
		return null;
	}
	
	private static Question createString() {
		Reader reader = new Reader();
		System.out.print("enter question:");
		String question = reader.readLn();
		
		System.out.print("enter answer:");
		String answer = reader.readLn();
		
		return new QuestionStringAnswer(question,answer);
	}

	private static Question createManyAnswers() {
		Reader reader = new Reader();
		System.out.print("enter question:");
		
		String question = reader.readLn();
		
		System.out.print("enter count of answers:");
		int countOfAnswers = reader.readInt();
		
		String[] answers = new String[countOfAnswers];
		
		reader.readLn();
		
		for(int i = 0 ; i < answers.length ; i++){
			System.out.print("enter answer:");
			answers[i] = reader.readLn();
		}
		System.out.print("enter count of correct answers:");
		int countOfCorrectAnswers = reader.readInt();
		
		
		Integer[] correctAnswers = new Integer[countOfCorrectAnswers];
		
		for(int i = 0 ; i < correctAnswers.length ; i++){
			System.out.print("enter index of correct answer:");
			correctAnswers[i] = reader.readInt();
		}
		return  new QuestionManyAnswers(question,answers,correctAnswers);
	}

	private static Question createOneAnswer() {
		Reader reader = new Reader();
		System.out.print("enter question:");
		String question = reader.readLn();
		System.out.print("enter count of answers:");
		int countOfAnswers = reader.readInt();
		
		reader.readLn();
		String[] answers = new String[countOfAnswers];
		for(int i = 0 ; i < answers.length ; i++){
			System.out.print("enter answer:");
			answers[i] = reader.readLn();
		}		
		System.out.print("enter index of correct answer:");
		int indexOfCorrectAnswer = reader.readInt();

		return new QuestionOneAnswer(question,answers,indexOfCorrectAnswer);
	}
}
